<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<el-button type="primary" icon="el-icon-plus" @click="add"></el-button>
				<el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length==0" @click="batch_del"></el-button>
			</div>
			<div class="right-panel">
				<div class="right-panel-search">
					<el-input v-model="search.keyword" placeholder="商户名称" clearable></el-input>
					<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
				</div>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" :apiObj="apiObj" row-key="id" @selection-change="selectionChange" stripe>
				<el-table-column type="selection" width="50"></el-table-column>
				<el-table-column label="商户名" prop="merchantName" width="200"></el-table-column>
				<el-table-column label="商户类型" prop="merchantType" width="150">
					<template #default="scope">
						<el-tag effect="plain" v-if="scope.row.merchantType==10">支付宝</el-tag>
						<el-tag effect="plain" type="success" v-if="scope.row.merchantType==20">微信</el-tag>
						<el-tag effect="plain" type="danger" v-if="scope.row.merchantType==30">银联</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="平台商户号" prop="platformMerchantNo" width="150"></el-table-column>
				<el-table-column label="状态" prop="status" width="200">
					<template #default="scope">
						<el-tag effect="plain" type="danger" v-if="scope.row.status==0">停用</el-tag>
						<el-tag effect="plain" type="success" v-if="scope.row.status==1">启用</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="创建时间" prop="createdTime" width="180"></el-table-column>
				<el-table-column label="操作" fixed="right" align="center" width="300">
					<template #default="scope">
						<el-button-group>
							<el-button text type="primary" size="small" @click="table_show(scope.row, scope.$index)">查看</el-button>
							<el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)">编辑</el-button>
							<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
								<template #reference>
									<el-button text type="primary" size="small">删除</el-button>
								</template>
							</el-popconfirm>
							<el-button text type="primary" v-if="scope.row.status==0" size="small" @click="updateStatus(scope.row.id, 1)">启用</el-button>
							<el-button text type="primary" v-if="scope.row.status==1" size="small" @click="updateStatus(scope.row.id, 0)">停用</el-button>
						</el-button-group>
					</template>
				</el-table-column>

			</scTable>
		</el-main>
	</el-container>

	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess" @closed="dialog.save=false"></save-dialog>

</template>

<script>
	import saveDialog from './save'

	export default {
		name: 'role',
		components: {
			saveDialog
		},
		data() {
			return {
				dialog: {
					save: false
				},
				apiObj: this.$API.merchant.page,
				selection: [],
				search: {
					keyword: null
				}
			}
		},
		methods: {
			//添加
			add(){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open()
				})
			},
			//编辑
			table_edit(row){
				this.dialog.save = true
				this.$nextTick(async () => {
					var params={id: row.id}
					var res = await this.$API.merchant.detail.get(params);
					if(res.code == 0){
						this.$refs.saveDialog.open('edit').setData(res.data)
					}else{
						this.$alert(res.message, "提示", {type: 'error'})
					}
				})
			},
			//查看
			table_show(row){
				this.dialog.save = true
				this.$nextTick(async () => {
					var params={id: row.id}
					var res = await this.$API.merchant.detail.get(params);
					if(res.code == 0){
						this.$refs.saveDialog.open('show').setData(res.data)
					}else{
						this.$alert(res.message, "提示", {type: 'error'})
					}
				})
			},
			// 修改商户状态
			async updateStatus(id, status){
				var data = {
					id: id,
					status: status
				}
				var res = await this.$API.merchant.updateStatus.post(data);
				if(res.code == 0){
					this.$refs.table.refresh()
					this.$message.success("商户状态修改成功！")
				}else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
			},
			//删除
			async table_del(row){
				var data = {
					ids: [row.id]
				}
				var res = await this.$API.merchant.deletes.post(data);
				if(res.code == 0){
					this.$refs.table.refresh()
					this.$message.success("删除成功")
				}else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
			},
			//批量删除
			async batch_del(){
				this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
					type: 'warning'
				}).then(async () => {
					const loading = this.$loading();
					var ids = []
					this.selection.forEach((item, index) => {
						ids[index] = item.id
					});
					var data = {
						ids: ids
					}
					var res = await this.$API.merchant.deletes.post(data);
					if(res.code == 0){
						this.$refs.table.refresh()
						this.$message.success("删除成功")
					}else{
						this.$alert(res.message, "提示", {type: 'error'})
					}
					loading.close();
				}).catch(() => {

				})
			},
			//表格选择后回调事件
			selectionChange(selection){
				this.selection = selection;
			},
			//表格内开关
			changeSwitch(val, row){
				row.status = row.status == '1'?'0':'1'
				row.$switch_status = true;
				setTimeout(()=>{
					delete row.$switch_status;
					row.status = val;
					this.$message.success("操作成功")
				}, 500)
			},
			//搜索
			upsearch(){
				this.$refs.table.upData(this.search)
			},
			//本地更新数据
			handleSaveSuccess(data, mode){
				if(mode=='add'){
					this.$refs.table.refresh()
				}else if(mode=='edit'){
					this.$refs.table.refresh()
				}
			}
		}
	}
</script>

<style>
</style>
